<?php

function isPlatezhBuy(&$platezh)
{
	return in_array($platezh['type'], array('buy', 'buy.drugoy', 'buy.des', 'buy.other'));
}


function fixCanEdit(&$fix_user_id, &$user)
{
	if(Auth::isSupport()) return true;
	static $root_id;
	$CI = &get_instance();
	if(!$root_id)
	{
		$root = $CI->MUser->getRoot();
		$root_id = $root['id'];
	}
	if($user['id'] == $root_id) return true;
	if(!@$user['id'] || !@$fix_user_id || $user['id'] == $fix_user_id) return false;
	else return true;
}


function fixCanDelete(&$fix, &$user)
{
	return fixCanEdit($fix, $user);
}


function platezhFromTo(&$p)
{
	$CI = &get_instance();
	$user_from = $CI->MUser->get($p['from_user_id']);
	$user_to = $CI->MUser->get($p['to_user_id']);
	switch($p['type'])
	{
		case "buy": 
		case "sell": 
		case "reklama": 
			$p['user_from'] = @$user_from['fio'];
			$p['user_to'] = @$user_to['fio'];
			break;

		case "buy.drugoy": 
		case "buy.des": 
		case "buy.other": 
			$p['user_from'] = $p['people'];
			$p['user_to'] = @$user_to['fio'];
			break;

		case "sell.drugoy": 
		case "sell.des": 
		case "sell.other": 
			$p['user_from'] = @$user_from['fio'];
			$p['user_to'] = $p['people'];
			break;

		case "komiss": 
			$p['user_from'] = @$user_from['fio'];
			$p['user_to'] = "Списание комиссии банка";
			break;
	}
}


function isMyPlatezh(&$platezh)
{
	if(!@$platezh['id']) return true;
	if(isPlatezhBuy($platezh) && $platezh['to_user_id'] == Auth::$user_id) return true;
	if(!isPlatezhBuy($platezh) && $platezh['from_user_id'] == Auth::$user_id) return true;
	return false;
}
